package es.ucm.fdi.xtremeaudio.vista;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.LineBorder;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import es.ucm.fdi.xtremeaudio.controlador.Controlador;

/**
 * @author ucm-is-grupo7-2011
 * 
 */
public class FormAyuda {

	private JFrame ventana;
	private JPanel panelVentana;
	private JPanelImagen panelSuperior;
	private JLabel labelTitulo;
	private JButton botonCerrar;
	private JButton botonMin;
	private JEditorPane panelHTML;
	private JScrollPane scrollPaneHTML;

	private static Integer ancho;
	private static Integer alto;

	public FormAyuda() {
		initLookAndFeel();
		initComponents();
	}

	private void initLookAndFeel() {
		// Establece el Look&Feel
		UIManager.put("nimbusBase", new Color(245, 94, 0));
		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (Exception e) {
			try {
				UIManager.setLookAndFeel(UIManager
						.getSystemLookAndFeelClassName());
			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			} catch (InstantiationException e1) {
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				e1.printStackTrace();
			} catch (UnsupportedLookAndFeelException e1) {
				e1.printStackTrace();
			}
		}

		// Obtiene las dimensiones de la pantalla
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		if (((float) screen.width / (float) screen.height) > 1.25){ // Panorámica
			ancho = new Integer((int) (screen.width / 2.8));
			alto = new Integer((int) (screen.height / 2.8));
		} else {
			ancho = new Integer((int)(screen.width / 2.8));
			alto = new Integer((int)(screen.height / 3.5));
		}		
	}

	private void initComponents() {
		ventana = new JFrame();
		panelVentana = new JPanel();
		panelSuperior = new JPanelImagen("img/fondo_1100x60.png");
		labelTitulo = new JLabel();
		botonMin = new JButton();
		botonCerrar = new JButton();
		scrollPaneHTML = new JScrollPane();
		try {
			panelHTML = new JEditorPane(FormAyuda.class.getClassLoader()
					.getResource("doc/help/Portada.html"));
		} catch (IOException e) {
			e.printStackTrace();
		}

		/*--- DIMENSIONES DE PANELES EN FUNCION DE LA PANTALLA ---*/
		// w->width, h->height
		final Integer wPVentana, wPSuperior, wPHTML;
		final Integer hPVentana, hPSuperior, hPHTML;
		final String sWPVentana, sWPSuperior, sWPHTML;
		final String sHPVentana, sHPSuperior, sHPHTML;
		Integer wIzq, wDer, hSup, hInf; // Auxiliares

		// 0: Panel Ventana
		wPVentana = ancho;
		sWPVentana = wPVentana.toString() + "dlu";

		hPVentana = alto;
		hSup = new Integer((int) (hPVentana * 0.15)); // Panel superior
		hInf = new Integer((int) (hPVentana * 0.85)); // Panel HTML
		sHPVentana = hSup.toString() + "dlu," + hInf.toString() + "dlu";

		// 1a: Panel Superior
		wPSuperior = wPVentana;
		wIzq = new Integer((int) (wPSuperior * 0.88)); // Logo
		wDer = new Integer((int) ((wPSuperior * 0.1) / 2)); // Tres botones
		sWPSuperior = wIzq.toString() + "dlu," + wDer.toString() + "dlu,"
				+ wDer.toString() + "dlu";

		hPSuperior = hSup;
		sHPSuperior = hPSuperior.toString() + "dlu";

		// 1b: Panel HTML
		wPHTML = wPVentana;
		sWPHTML = wPHTML.toString() + "dlu";

		hPHTML = hInf;
		sHPHTML = hPHTML.toString() + "dlu";

		// ======= Ventana =======
		ventana.setUndecorated(true); // Quita los botones del borde
		ventana.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		ventana.setAlwaysOnTop(false);
		ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Image icono = Toolkit.getDefaultToolkit().getImage(
				FormAyuda.class.getClassLoader().getResource(
						"img/iconoX_16x16.png"));
		ventana.setIconImage(icono);
		ventana.setTitle("Ayuda - XtremeAudio");
		Container ventanaContentPane = ventana.getContentPane();
		ventanaContentPane.setBackground(Color.black);
		ventanaContentPane.setLayout(new FormLayout(ancho.toString() + "dlu",
				alto.toString() + "dlu"));

		// ======== panelVentana ========
		{
			MoverPanelListener mml = new MoverPanelListener(panelVentana);
			panelVentana.addMouseListener(mml);
			panelVentana.addMouseMotionListener(mml);
			panelVentana
					.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			panelVentana.setBackground(Color.black);
			panelVentana.setBorder(new LineBorder(new Color(255, 157, 92), 2,
					true));
			panelVentana.setLayout(new FormLayout(sWPVentana, sHPVentana));

			// ======== panelSuperior ========
			{
				panelSuperior.setBackground(new Color(255, 102, 0));
				panelSuperior.setBorder(new LineBorder(new Color(255, 157, 92),
						2, true));
				panelSuperior
						.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
				panelSuperior
						.setLayout(new FormLayout(sWPSuperior, sHPSuperior));

				// ---- labelTitulo ----
				labelTitulo.setIcon(new ImageIcon(FormAyuda.class
						.getClassLoader().getResource(
								"img/logoAyuda_350x50.png")));
				labelTitulo
						.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
				labelTitulo.setFocusable(false);
				panelSuperior.add(labelTitulo, new CellConstraints(1, 1, 1, 1,
						CellConstraints.CENTER, CellConstraints.TOP,
						new Insets(5, 0, 0, 0)));

				// ---- botonMin ----
				botonMin.setIcon(new ImageIcon(FormAyuda.class.getClassLoader()
						.getResource("img/minimizar_45x45.png")));
				botonMin.setCursor(Cursor
						.getPredefinedCursor(Cursor.HAND_CURSOR));
				botonMin.setBackground(Color.black);
				botonMin.setToolTipText("Minimizar");
				botonMin.setFocusable(false);
				botonMin.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
				botonMin.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						botonMinActionPerformed(e);
					}

					private void botonMinActionPerformed(ActionEvent e) {
						Controlador.minimizarAyuda();
					}
				});
				panelSuperior.add(botonMin, new CellConstraints(2, 1, 1, 1,
						CellConstraints.CENTER, CellConstraints.TOP,
						new Insets(2, 0, 0, 0)));

				// ---- botonCerrar ----
				botonCerrar.setIcon(new ImageIcon(FormAyuda.class
						.getClassLoader().getResource("img/cerrar_45x45.png")));
				botonCerrar.setCursor(Cursor
						.getPredefinedCursor(Cursor.HAND_CURSOR));
				botonCerrar.setBackground(Color.black);
				botonCerrar.setToolTipText("Salir");
				botonCerrar.setFocusable(false);
				botonCerrar
						.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
				botonCerrar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						botonCerrarActionPerformed(e);
					}

					private void botonCerrarActionPerformed(ActionEvent e) {
						Controlador.cerrarAyuda();
					}
				});
				panelSuperior.add(botonCerrar, new CellConstraints(3, 1, 1, 1,
						CellConstraints.CENTER, CellConstraints.TOP,
						new Insets(2, 0, 0, 0)));
			}
			panelVentana.add(panelSuperior, new CellConstraints(1, 1, 1, 1,
					CellConstraints.FILL, CellConstraints.FILL, new Insets(3, 2, 3, 6)));

			// ======== scrollPaneHTML ========
			{
				scrollPaneHTML.setBorder(null);
				scrollPaneHTML
						.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
				scrollPaneHTML
						.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
				scrollPaneHTML
						.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
				scrollPaneHTML.setViewportBorder(null);

				// ======== panelHTML ========
				{
					panelHTML.setBackground(new Color(255, 102, 0));
					panelHTML.setBorder(new LineBorder(new Color(255, 157, 92),	2, true));
					panelHTML.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
					panelHTML.setEditable(false);
					panelHTML.setContentType("text/html");

					// Listener para seguir hipervinculos
					panelHTML.addHyperlinkListener(new HyperlinkListener() {
						@Override
						public void hyperlinkUpdate(HyperlinkEvent e) {
							if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
								// JEditorPane pane = (JEditorPane)
								// e.getSource();
								if (e instanceof HTMLFrameHyperlinkEvent) {
									HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
									HTMLDocument doc = (HTMLDocument) panelHTML
											.getDocument();
									doc.processHTMLFrameHyperlinkEvent(evt);
								} else {
									try {
										panelHTML.setPage(e.getURL());
										scrollPaneHTML
												.setViewportView(panelHTML);
									} catch (Throwable t) {
										t.printStackTrace();
									}
								}
							}
						}
					});
					panelHTML.setLayout(new FormLayout(sWPHTML, sHPHTML));
				}
				scrollPaneHTML.setViewportView(panelHTML);
			}
			panelVentana.add(scrollPaneHTML, new CellConstraints(1, 2, 1, 1,
					CellConstraints.FILL, CellConstraints.FILL, new Insets(1, 2, 4, 6)));
		}
		ventanaContentPane.add(panelVentana, new CellConstraints(1, 1, 1, 1,
				CellConstraints.FILL, CellConstraints.FILL));
		ventana.pack();
		ventana.setLocationRelativeTo(null);
	}

	/*--- METODOS PUBLICOS ---*/
	public void show() {
		try {
			panelHTML.setPage(FormAyuda.class.getClassLoader().getResource(
					"doc/help/Portada.html"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		ventana.setVisible(true);
	}

	public void hide() {
		ventana.setVisible(false);
	}

	public JFrame getVentana() {
		return ventana;
	}

	public void setVentana(JFrame ventana) {
		this.ventana = ventana;
	}

	public JButton getBotonCerrar() {
		return botonCerrar;
	}

	public void setBotonCerrar(JButton botonCerrar) {
		this.botonCerrar = botonCerrar;
	}

	public JEditorPane getPanel() {
		return panelHTML;
	}

	public void setPanel(JEditorPane panelHTML) {
		this.panelHTML = panelHTML;
	}

	public JPanel getPanelVentana() {
		return panelVentana;
	}

	public void setPanelVentana(JPanel panelVentana) {
		this.panelVentana = panelVentana;
	}

	public JPanelImagen getPanelSuperior() {
		return panelSuperior;
	}

	public void setPanelSuperior(JPanelImagen panelSuperior) {
		this.panelSuperior = panelSuperior;
	}

	public JLabel getLabelTitulo() {
		return labelTitulo;
	}

	public void setLabelTitulo(JLabel labelTitulo) {
		this.labelTitulo = labelTitulo;
	}

	public JButton getBotonMin() {
		return botonMin;
	}

	public void setBotonMin(JButton botonMin) {
		this.botonMin = botonMin;
	}

	public JEditorPane getPanelHTML() {
		return panelHTML;
	}

	public void setPanelHTML(JEditorPane panelHTML) {
		this.panelHTML = panelHTML;
	}

	public static Integer getAncho() {
		return ancho;
	}

	public static void setAncho(Integer ancho) {
		FormAyuda.ancho = ancho;
	}

	public static Integer getAlto() {
		return alto;
	}

	public static void setAlto(Integer alto) {
		FormAyuda.alto = alto;
	}

	public JScrollPane getScrollPaneHTML() {
		return scrollPaneHTML;
	}

	public void setScrollPaneHTML(JScrollPane scrollPaneHTML) {
		this.scrollPaneHTML = scrollPaneHTML;
	}

}
